package com.jsn.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.jsn.entity.SmartUser;
import com.jsn.entity.UserDetail;
import org.bouncycastle.crypto.generators.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserDetailService implements UserDetailsService {

    @Autowired
    @Lazy
    private UserService userService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        com.jsn.entity.User one = userService.getOne(new QueryWrapper<com.jsn.entity.User>()
                .lambda().eq(com.jsn.entity.User::getUsername, s));

        if (one != null) {
            List<GrantedAuthority> grantedAuthorities = Lists.newArrayList();
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("/admin/index");
            grantedAuthorities.add(simpleGrantedAuthority);
            SmartUser user = new SmartUser();
            user.setUsername(s);
            user.setAuthorities(grantedAuthorities);
            user.setPassword(one.getPassword());
            user.setId(one.getId());
            return user;
        }


        return null;

    }


}
